// //index.js
 Page({
  data: {
    flower:[],
    num : 1,
    flowerrd:[],
    cart1:{
      genre: "",
      gross: 0,
      image: "",
      price: 0,
      productsell: 0,
      title: "",
      _id: 0,
      num:0,
      checked:false
    }

  },
  //预览大图
  // previewImage:function(e){
  //   var current = e.target.dataset.src;

  //   wx.previewImage({
  //     current: current,
  //     urls: this.data.flower[0].image
  //   })
  // },
  //收藏
  addLike(){
    this.setData({
      isLike: !this.data.isLike
    })
  },
//   //跳转首页
  toCar(){
    wx.switchTab({
      url: '../index/index',
    })
  },
//   //立即购买
/*   immeBuy(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../pay/index?id='+id,
    })
   }, */
  onLoad: function(option) {
    let {id} = option
    this.getFlower(id)
    this.getFlowerRd()
  },

  getFlower:function(flower_id){
    var that = this
   const db = wx.cloud.database()
   db.collection('flower').where({
        _id:flower_id
      }).get({
        success:function(res){
          that.setData({
            flower:res.data
          })
        },
        fail:function(err){
          console.log(err)
      }
    })
 },
//点击我显示底部弹出框
clickme:function(){
  this.showModal();
},

//显示对话框
 showModal: function () {
   // 显示遮罩层
   var animation = wx.createAnimation({
     duration: 200,
     timingFunction: "linear",
     delay: 0
   })
   this.animation = animation
   animation.translateY(300).step()
   this.setData({
     animationData: animation.export(),
     showModalStatus: true
   })
   setTimeout(function () {
     animation.translateY(0).step()
     this.setData({
       animationData: animation.export()
     })
   }.bind(this), 200)
 },
 //隐藏对话框
 hideModal: function () {
   // 隐藏遮罩层
   var animation = wx.createAnimation({
     duration: 200,
     timingFunction: "linear",
     delay: 0
   })
   this.animation = animation
   animation.translateY(300).step()
   this.setData({
     animationData: animation.export(),
   })
   setTimeout(function () {
     animation.translateY(0).step()
     this.setData({
       animationData: animation.export(),
       showModalStatus: false
     })
   }.bind(this), 200)
 },
/* 加数 */
addCount: function (e) {
  console.log("刚刚您点击了加1");
  var num = this.data.num;
  // 总数量-1  
  if (num < 1000) {
    this.data.num++;
  }
  // 将数值与状态写回  
  this.setData({
    num: this.data.num
  });
},
/* 减数 */
delCount: function (e) {
  console.log("刚刚您点击了减1");
  var num = this.data.num;
  // 商品总数量-1
  if (num > 1) {
    this.data.num--;
  }
  // 将数值与状态写回  
  this.setData({
    num: this.data.num
  });
},
getCount: function (e) {
  var num = this.data.num;
  //获取缓存中的购物车数组
  this.obj = this.data.flower
  let cart = wx.getStorageSync('cart')||[];
  let right = this.data.flower
  //判断商品对象是否在购物车数组中、
  let index = e.target.dataset.id; 
  let that = this
  let id = cart.findIndex(v=>v._id === index)
  if(id === -1){
    const db = wx.cloud.database()
    db.collection('flower').where({
      _id:index
    }).get({
      success(res){
        console.log(res);
        that.setData({
          cart1:{
           genre: res.data[0].genre,
           gross: res.data[0].gross,
           image: res.data[0].image,
           price: res.data[0].price,
           productsell: res.data[0].productsell,
           title: res.data[0].title,
           _id: res.data[0]._id,
           num:num,
           checked:false
          }
    })
    cart.push(that.data.cart1)
    wx.setStorageSync("cart", cart);
  }
})
  
  }else{
    cart[id].num++;
    wx.setStorageSync("cart", cart);
  }
  

  wx.showToast({
    title: "添加成功",
    icon: 'success'
  })
},

getFlowerRd:function(){
  var that = this
 const db = wx.cloud.database()
 db.collection('flower').orderBy('productsell','desc').limit(4).get({
     success:function(res){
       
       that.setData({
         flowerrd:res.data
       })
     },
     fail:function(err){
       console.log(err)
     }
   })
},


   })
